import 'package:flutter/material.dart';
import 'package:scanner_example/hardware_keyboard_listener.dart';
import 'package:scanner_example/scan_listener.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String v1 = '';
  String v2 = '';
  HardwareKeyboardListener? scanListenerV2;
  ScanListener? scanListenerV1;
  @override
  void initState() {
    super.initState();
    scanListenerV2 = HardwareKeyboardListener(onScanV2);
    scanListenerV1 = ScanListener(onScanV1);
  }

  void onScanV2(dynamic event) {
    setState(() {
      v2 += event + "\n";
    });
  }

  void onScanV1(dynamic event) {
    setState(() {
      v1 += event + "\n";
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('测试扫码'),
        ),
        body: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Expanded(
              flex: 1,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text('V1扫码方式'),
                  Expanded(
                    child: Text(
                      v1,
                      maxLines: 30,
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              flex: 1,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  const Text('V2扫码方式'),
                  Expanded(
                    child: Text(
                      v2,
                      maxLines: 30,
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
